using System.Collections.Generic;
using FireStarter.Base;
using FireStarter.CodeGeneration.Generator;
using FireStarter.CodeGeneration.Parser;
using FireStarterModeller.BO;

namespace FireStarterModeller.Logic
{
    public class  DMToEntityClassDefsXmlConverter
    {
        public string ConvertToXml(DMClass dmClass)
        {
            List<IActionParser> actionParsers = StatementParser.GetDefaultActionParsers();
            actionParsers.Add(new IncludeExpressionParser(new ResourceTemplateLoader(this.GetType().Assembly, "FireStarterModeller.Logic.ClassDefXmlTemplate")));
            return ConvertToXml(dmClass, ClassDefXmlTemplate.SingleClassDefXmlTemplate_TypeParameter_Text, actionParsers);
        }

        public string ConvertToXml(DMSolution solution)
        {
            List<IActionParser> actionParsers = StatementParser.GetDefaultActionParsers();
            actionParsers.Add(new IncludeExpressionParser(new ResourceTemplateLoader(this.GetType().Assembly, "FireStarterModeller.Logic.ClassDefXmlTemplate")));
            return ConvertToXml(solution, ClassDefXmlTemplate.ClassDefXmlTemplate_TypeParameter_Text, actionParsers);
        }

        private string ConvertToXml(IModelElement element, string template, List<IActionParser> actionParsers)
        {
            ModelStrategyMapper modelStrategyMapper = new ModelStrategyMapper();
            modelStrategyMapper.SetStrategy(typeof(IModelElement), new ReflectionGenerationStrategy());

            StatementParser statementParser = new StatementParser();
            TemplateExpression templateExpression = statementParser.Parse(ref template, actionParsers);
            string xmlForSolution = templateExpression.Generate(modelStrategyMapper, element);
            return xmlForSolution;
        }
    }
}